package fu.capstone.hanoicityguide.dataaccess.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import fu.capstone.hanoicityguide.business.model.Category;
import fu.capstone.hanoicityguide.common.utility.DAOUtility;

public class BaseDAOImpl implements BaseDAO {
	//

	// public static final String KEY_NAME = "name";
	// public static final String KEY_PHONE = "phone";
	// public static final String KEY_EMAIL = "email";

	private DatabaseHelper dbHelper;
	private SQLiteDatabase database;

	private final Context mContext;

	public BaseDAOImpl(Context ctx) {
		this.mContext = ctx;
	}

	// connection to database function
	@Override
	public DatabaseHelper connectDB() {
		try {

			DatabaseHelper dbHelper = new DatabaseHelper(mContext,
					DatabaseHelper.DATABASE_NAME, null,
					DatabaseHelper.DATABASE_VERSION);
			// database = dbHelper.getWritableDatabase();
			return dbHelper;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void closeDB(SQLiteDatabase database, DatabaseHelper dbHelper) {
		try {
			dbHelper.close();
			database.close();
		} catch (Exception e) {
			Log.v("Can't close database !", e.toString());
		}
	}

	// insert HOME into sqlite database
	@Override
	public long insertHome(int homeId, String overview, String history,
			int albumImageId) {
		long result = 0;
		DAOUtility daoUtility = new DAOUtility();
		// validate before insert to database
		if (daoUtility.validateInsertHome(homeId, overview, history,
				albumImageId)) {
			// check connection
			if ((dbHelper = connectDB()) != null) {
				try {
					// try to insert
					database = dbHelper.getWritableDatabase();
					ContentValues inititalValues = new ContentValues();
					inititalValues.put("Id", homeId);
					inititalValues.put("Overview", overview);
					inititalValues.put("History", history);
					inititalValues.put("AlbumImageId", albumImageId);
					result = database.insert("Home", null, inititalValues);
				} catch (Exception e) {
					// catch exception and show log
					Log.v("Can't insert Home to DB", e.toString());
					result = -1;
				} finally {
					// close database
					closeDB(database, dbHelper);
				}
			} else {
				result = -1;
			}
		} else {
			result = -1;
		}
		// if can't insert this function will return -1 else return != -1
		return result;
	}

	// insert CATEGORY into sqlite database
	@Override
	public long insertCategory(int categoryID, String categoryName) {
		long result = 0;
		DAOUtility daoUtility = new DAOUtility();
		// validate before insert to database
		if (daoUtility.validateInsertCategory(categoryID, categoryName)) {
			// check connection
			if ((dbHelper = connectDB()) != null) {
				try {
					// try to insert
					database = dbHelper.getWritableDatabase();
					ContentValues inititalValues = new ContentValues();
					inititalValues.put("Id", categoryID);
					inititalValues.put("CategoryName", categoryName);
					result = database.insert("Category", null, inititalValues);
				} catch (Exception e) {
					// catch exception and show log
					Log.v("Can't insert category to DB", e.toString());
					result = -1;
				} finally {
					// close database
					closeDB(database, dbHelper);
				}
			} else {
				result = -1;
			}
		} else {
			result = -1;
		}
		// if can't insert this function will return -1 else return != -1
		return result;
	}

	// insert TYPE into sqlite database
	@Override
	public long insertType(int typeID, int categoryID, String typeName) {
		long result = 0;
		DAOUtility daoUtility = new DAOUtility();
		// validate before insert to database
		if (daoUtility.validateInsertType(typeID, categoryID, typeName)) {
			// check connection
			if ((dbHelper = connectDB()) != null) {
				try {
					// try to insert
					database = dbHelper.getWritableDatabase();
					ContentValues inititalValues = new ContentValues();
					inititalValues.put("Id", typeID);
					inititalValues.put("CategoryId", categoryID);
					inititalValues.put("TypeName", typeName);
					result = database.insert("Type", null, inititalValues);
				} catch (Exception e) {
					// catch exception and show log
					Log.v("Can't insert Type to DB", e.toString());
					result = -1;
				} finally {
					// close database
					closeDB(database, dbHelper);
				}
			} else {
				result = -1;
			}
		} else {
			result = -1;
		}
		// if can't insert this function will return -1 else return != -1
		return result;
	}

	// insert ALBUMIMAGE into sqlite database
	@Override
	public long insertAlbumImage(int albumImageId, String iconPath,
			ArrayList<String> imgPath) {
		long result = 0;
		DAOUtility daoUtility = new DAOUtility();
		// validate before insert to database
		if (daoUtility.validateInsertAlbumImage(albumImageId, iconPath,
				imgPath.get(0), imgPath.get(1), imgPath.get(2), imgPath.get(3),
				imgPath.get(4))) {
			if ((dbHelper = connectDB()) != null) {
				try {
					database = dbHelper.getWritableDatabase();
					ContentValues inititalValues = new ContentValues();
					inititalValues.put("Id", albumImageId);
					inititalValues.put("IconPath", iconPath);
					inititalValues.put("ImagePath1", imgPath.get(0));
					inititalValues.put("ImagePath2", imgPath.get(1));
					inititalValues.put("ImagePath3", imgPath.get(2));
					inititalValues.put("ImagePath4", imgPath.get(3));
					inititalValues.put("ImagePath5", imgPath.get(4));
					result = database
							.insert("AlbumImage", null, inititalValues);
				} catch (Exception e) {
					Log.v("Can't insert AlbumImage to DB", e.toString());
					result = -1;
				} finally {
					closeDB(database, dbHelper);
				}
			} else {
				result = -1;
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert RATING into sqlite database
	@Override
	public long insertRating(int ratingId, int ratingValue) {
		long result = 0;
		DAOUtility daoUtility = new DAOUtility();
		if (daoUtility.validateInsertRating(ratingId, ratingValue)) {
			if ((dbHelper = connectDB()) != null) {
				try {
					database = dbHelper.getWritableDatabase();
					ContentValues inititalValues = new ContentValues();
					inititalValues.put("Id", ratingId);
					inititalValues.put("RatingValue", ratingValue);
					result = database.insert("Rating", null, inititalValues);
				} catch (Exception e) {
					Log.v("Can't insert Rating to DB", e.toString());
					result = -1;
				} finally {
					closeDB(database, dbHelper);
				}
			} else {
				result = -1;
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert GEOLOCATION into sqlite database
	@Override
	public long insertGeolocation(int geolocationID, double geolocationLong,
			double geolocationLat) {
		long result = 0;
		DAOUtility daoUtility = new DAOUtility();
		// validate before insert to database
		if (daoUtility.validateInsertGeoLocation(geolocationID,
				geolocationLong, geolocationLat)) {
			if ((dbHelper = connectDB()) != null) {
				try {
					database = dbHelper.getWritableDatabase();
					ContentValues inititalValues = new ContentValues();
					inititalValues.put("Id", geolocationID);
					inititalValues.put("Longitude", geolocationLong);
					inititalValues.put("Latitude", geolocationLat);
					result = database.insert("Geolocation", null,
							inititalValues);
				} catch (Exception e) {
					Log.v("Can't insert Geolocation to DB", e.toString());
					result = -1;
				} finally {
					closeDB(database, dbHelper);
				}
			} else {
				result = -1;
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert CLASS into sqlite database
	@Override
	public long insertClass(int classId, String className) {
		long result = 0;
		DAOUtility daoUtility = new DAOUtility();
		// validate before insert to database
		if (daoUtility.validateInsertClass(classId, className)) {
			if ((dbHelper = connectDB()) != null) {
				try {
					database = dbHelper.getWritableDatabase();
					ContentValues inititalValues = new ContentValues();
					inititalValues.put("Id", classId);
					inititalValues.put("ClassName", className);
					result = database.insert("Class", null, inititalValues);
				} catch (Exception e) {
					Log.v("Can't insert Class to DB", e.toString());
					result = -1;
				} finally {
					closeDB(database, dbHelper);
				}
			} else {
				result = -1;
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert PLACE into sqlite database
	@Override
	public long insertPlace(int placeId, String placeName, int geoId,
			int categoryId, int typeId, int albumImageId, int ratingId,
			int classId, float placePrice, String placeDescription,
			String placeAddress, String placePhone, String placeWebsite,
			String openTime, String closeTime, int dbVersionId) {
		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", placeId);
				inititalValues.put("Name", placeName);
				inititalValues.put("GeoId", geoId);
				inititalValues.put("CategoryId", categoryId);
				inititalValues.put("TypeId", typeId);
				inititalValues.put("AlbumImageId", albumImageId);
				inititalValues.put("RatingId", ratingId);
				// inititalValues.put("", value);

				if (0 == classId) {
					inititalValues.putNull("ClassId");
				} else {
					inititalValues.put("ClassId", classId);
				}

				if (0 == placePrice) {
					inititalValues.putNull("Price");
				} else {
					inititalValues.put("Price", placePrice);
				}

				inititalValues.put("Description", placeDescription);

				if ("".equals(openTime)) {
					inititalValues.putNull("OpenTime");
				} else {
					inititalValues.put("OpenTime", openTime);
				}

				if ("".equals(closeTime)) {
					inititalValues.putNull("CloseTime");
				} else {
					inititalValues.put("CloseTime", closeTime);
				}

				inititalValues.put("Address", placeAddress);

				if ("".equals(placePhone)) {
					inititalValues.putNull("PhoneNumber");
				} else {
					inititalValues.put("PhoneNumber", placePhone);
				}

				if ("".equals(placeWebsite)) {
					inititalValues.putNull("Website");
				} else {
					inititalValues.put("Website", placeWebsite);
				}

				inititalValues.put("DBVersionId", dbVersionId);

				// inititalValues.put("Comment", placeComment);
				// inititalValues.putNull("Comment");

				result = database.insert("Place", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert Place to DB", e.toString());
				result = 0;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert FOOD into sqlite database
	@Override
	public long insertFood(int foodID, String foodName, String foodDesc,
			int ratingID, int albumImageID, int geoId, float price,
			String address, String phoneNumber) {
		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", foodID);
				inititalValues.put("Name", foodName);
				inititalValues.put("Description", foodDesc);
				inititalValues.put("RatingID", ratingID);
				inititalValues.put("AlbumImageID", albumImageID);
				inititalValues.put("GeoId", geoId);
				inititalValues.put("Price", price);
				inititalValues.put("Address", address);
				inititalValues.put("PhoneNumber", phoneNumber);
				result = database.insert("Food", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert Food to DB", e.toString());
				result = 0;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert HOLIDAY into sqlite database
	@Override
	public long insertHoliday(int holidayID, String holidayName,
			String holidayDesc, int albumImageID) {
		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", holidayID);
				inititalValues.put("Name", holidayName);
				inititalValues.put("Description", holidayDesc);
				inititalValues.put("AlbumImageId", albumImageID);
				result = database.insert("Holiday", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert Holiday to DB", e.toString());
				result = 0;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// get list TYPE from database sqlite
	@Override
	public String[] getListType(int categoryID) {
		String[] arrType = null;
		Cursor cursor = null;
		int count = 1;
		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryCategory = "SELECT * FROM Type WHERE CategoryId == "
					+ categoryID;
			cursor = database.rawQuery(queryCategory, null);
			arrType = new String[cursor.getCount() + 1];
			arrType[0] = "All";
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						arrType[count] = cursor.getString(cursor
								.getColumnIndex("TypeName"));
						count++;
					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query Type !", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		return arrType;

	}

	// get list HOTELCLASS from database sqlite
	@Override
	public String[] getListHotelClass() {
		String[] arrHotelClass = null;
		Cursor cursor = null;
		int count = 1;
		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryHotelClass = "SELECT * FROM [Class] ";
			cursor = database.rawQuery(queryHotelClass, null);
			arrHotelClass = new String[cursor.getCount() + 1];
			arrHotelClass[0] = "All";
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						arrHotelClass[count] = cursor.getString(cursor
								.getColumnIndex("ClassName"));
						count++;
					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query Class !", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		return arrHotelClass;

	}

	// get list CATEGORY from database sqlite
	@Override
	public ArrayList<Category> getListCategory(String filter) {
		ArrayList<Category> arrCategory = new ArrayList<Category>();
		Cursor cursor = null;
		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryCategory;
			if (filter.equals("")) {
				queryCategory = "SELECT * FROM Category";
				// arrCategory.add(new Category(0, "All"));
			} else {
				queryCategory = "SELECT * FROM Category WHERE CategoryName == '"
						+ filter + "'";
			}

			cursor = database.rawQuery(queryCategory, null);

			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						arrCategory.add(new Category(cursor.getInt(cursor
								.getColumnIndex("Id")), cursor.getString(cursor
								.getColumnIndex("CategoryName"))));
					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query Category !", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		return arrCategory;

	}

	// get lastest dbversion id with update status equals Confirmed
	@Override
	public int getLastestDBVersionId() {
		int lastestVersion = 0;
		Cursor cursor = null;
		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryCategory;
			queryCategory = "SELECT DBVersion.[Id] FROM DBVersion WHERE DBVersion.[UpdateStatus] == \"Confirmed\" ORDER BY DBVersion.[Id] DESC LIMIT 1";

			cursor = database.rawQuery(queryCategory, null);

			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						lastestVersion = cursor.getInt(cursor
								.getColumnIndex("Id"));
					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query DBVersion !", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		return lastestVersion;
	}

	// insert TRANSPORT CATEGORY into sqlite database
	@Override
	public long insertTransportCategory(int transportCategoryID,
			String transportCategoryName) {
		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", transportCategoryID);
				inititalValues.put("TransportName", transportCategoryName);
				result = database.insert("TransportCategory", null,
						inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert TransportCategory to DB", e.toString());
				result = 0;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}
		return result;

	}

	// insert TRANSPORT into sqlite database
	@Override
	public long insertTransport(int transportID, int transportCategoryID,
			String transportName, String firstLeg, String secondLeg,
			String price, String phoneNumber, String website,
			String transportIconPath) {
		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", transportID);
				inititalValues.put("TransportId", transportCategoryID);
				inititalValues.put("Name", transportName);
				inititalValues.put("FirstLeg", firstLeg);
				inititalValues.put("SecondLeg", secondLeg);
				inititalValues.put("IconPath", transportIconPath);
				inititalValues.put("Price", price);
				inititalValues.put("PhoneNumber", phoneNumber);
				inititalValues.put("Website", website);
				result = database.insert("Transport", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert Transport to DB", e.toString());
				result = 0;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}
		return result;

	}

	// insert COMMON DIALOGUE into sqlite database
	@Override
	public long insertCommonDialogue(int dialogueId, String VietnameseContent,
			String EnglishContent, String audioPath) {

		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", dialogueId);
				inititalValues.put("VietnameseContent", VietnameseContent);
				inititalValues.put("EnglishContent", EnglishContent);
				inititalValues.put("AudioPath", audioPath);
				result = database
						.insert("CommonDialogue", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert Common Dialogue to DB", e.toString());
				result = 0;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert ITINERARY into sqlite database
	@Override
	public long insertItinerary(int itineraryId, String itineraryName,
			int ratingId, int albumImageId, int dbVersionId) {

		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", itineraryId);
				inititalValues.put("ItineraryName", itineraryName);
				inititalValues.put("RatingId", ratingId);
				inititalValues.put("AlbumImageId", albumImageId);
				inititalValues.put("DBVersionId", dbVersionId);
				result = database.insert("Itinerary", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert Itinerary to DB", e.toString());
				result = -1;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert ITINERARY DETAIL into sqlite database
	@Override
	public long insertItineraryDetail(int itineraryId, int placeId,
			int placeOder, int dBVersionId) {

		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("ItineraryId", itineraryId);
				inititalValues.put("PlaceId", placeId);
				inititalValues.put("PlaceOrder", placeOder);
				inititalValues.put("DBVersionId", dBVersionId);
				result = database.insert("ItineraryDetail", null,
						inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert ItineraryDetail to DB", e.toString());
				result = -1;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert TOUR into sqlite database
	@Override
	public long insertTour(int id, String name, int providerId, int ratingId,
			int albumImageId, float price, float duration, String desc,
			int dbVersionId) {
		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", id);
				inititalValues.put("Name", name);
				inititalValues.put("ProviderId", providerId);
				inititalValues.put("RatingId", ratingId);
				inititalValues.put("AlbumImageId", albumImageId);
				inititalValues.put("Price", price);
				inititalValues.put("Duration", duration);
				inititalValues.put("Description", desc);
				inititalValues.put("DBVersionId", dbVersionId);
				result = database.insert("Tour", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert Tour to DB", e.toString());
				result = -1;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert TOUR PROVIDER into sqlite database
	@Override
	public long insertTourProvider(int id, String name, String iconPath,
			String address, String website, String phoneNumber, int dbVersionId) {

		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", id);
				inititalValues.put("Name", name);
				inititalValues.put("IconPath", iconPath);
				inititalValues.put("Address", address);
				inititalValues.put("Website", website);
				inititalValues.put("PhoneNumber", phoneNumber);
				inititalValues.put("DBVersionId", dbVersionId);
				result = database.insert("TourProvider", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert TourProvider to DB", e.toString());
				result = -1;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// insert DBVERSION into sqlite database
	@Override
	public long insertDBVersion(int dbVersionId, String versionName,
			String updateStatus) {

		long result = 0;
		if ((dbHelper = connectDB()) != null) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues inititalValues = new ContentValues();
				inititalValues.put("Id", dbVersionId);
				inititalValues.put("VersionName", versionName);
				inititalValues.put("UpdateStatus", updateStatus);
				result = database.insert("DBVersion", null, inititalValues);
			} catch (Exception e) {
				Log.v("Can't insert DBVersion to DB", e.toString());
				result = -1;
			} finally {
				closeDB(database, dbHelper);
			}
		} else {
			result = -1;
		}

		return result;
	}

	// backup database method
	@Override
	public boolean backupDatabase() {
		boolean result = false;
		FileInputStream fis = null;
		OutputStream os = null;
		try {
			File file = mContext.getDatabasePath(DatabaseHelper.DATABASE_NAME);
			fis = new FileInputStream(file);

			// create dir if does not existed
			File mydir = mContext.getDir("backup", Context.MODE_PRIVATE);
			if (!mydir.exists()) {
				mydir.mkdirs();
			}
			String outputDB = "//data//" + "//data//"
					+ "fu.capstone.hanoicityguide" + "//app_backup//"
					+ DatabaseHelper.DATABASE_NAME;
			os = new FileOutputStream(outputDB);
			// Transfer bytes from the inputfile to the outputfile
			byte[] buffer = new byte[1024];
			int length;

			while ((length = fis.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			// Close the streams
			os.flush();
			os.close();
			fis.close();
			result = true;
		} catch (IOException e) {
			// TODO: handle exception
			result = false;
			Log.e("backupDatabase : ", e.toString());
		}
		return result;
	}

	// restore database method
	@Override
	public boolean restoreDatabase() {
		boolean result = false;
		FileInputStream fis = null;
		OutputStream os = null;
		try {
			String inputDB = "//data//" + "//data//"
					+ "fu.capstone.hanoicityguide" + "//app_backup//"
					+ DatabaseHelper.DATABASE_NAME;
			String outputDB = "//data//" + "//data//"
					+ "fu.capstone.hanoicityguide" + "//databases//"
					+ DatabaseHelper.DATABASE_NAME;

			fis = new FileInputStream(inputDB);
			os = new FileOutputStream(outputDB);

			// Transfer bytes from the inputfile to the outputfile
			byte[] buffer = new byte[1024];
			int length;

			while ((length = fis.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}

			File inputFile = new File(inputDB);
			if (inputFile.exists()) {
				inputFile.delete();
			}

			// Close the streams
			os.flush();
			os.close();
			fis.close();
			result = true;
		} catch (IOException e) {
			// TODO: handle exception
			result = false;
			Log.e("backupDatabase : ", e.toString());
		}
		return result;
	}

	// delete database method
	@Override
	public boolean deleteDatabase() {
		boolean result = false;
		try {
			String dbPath = "//data//" + "//data//"
					+ "fu.capstone.hanoicityguide" + "//databases//"
					+ DatabaseHelper.DATABASE_NAME;

			// check for dbFile existed
			File dbFile = new File(dbPath);
			if (dbFile.exists()) {
				dbFile.delete();
			}

			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			result = false;
			Log.e("deleteDatabase : ", e.toString());
		}
		return result;
	}

	// delete database method
	@Override
	public boolean deleteBackupDatabase() {
		boolean result = false;
		try {
			String dbPath = "//data//" + "//data//"
					+ "fu.capstone.hanoicityguide" + "//app_backup//"
					+ DatabaseHelper.DATABASE_NAME;

			// check for dbFile existed
			File dbFile = new File(dbPath);
			if (dbFile.exists()) {
				dbFile.delete();
			}

			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			result = false;
			Log.e("deleteBackupDatabase : ", e.toString());
		}
		return result;
	}
}
